
<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
            >
            <el-table-column label="用户编号" align="center" key="id" prop="id" />
            <el-table-column
                label="用户名称"
                align="center"
                prop="username"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="用户昵称"
                align="center"
                prop="nickname"
                :show-overflow-tooltip="true"
            />
            <el-table-column label="手机号码" align="center" prop="phone" width="120" />
            <el-table-column label="状态" key="status" width="100px">
              <template #default="scope">
                <el-switch
                    v-model="scope.row.status"
                    :active-value="1"
                    :inactive-value="0"
                    @change="handleStatusChange(scope.row)"
                />
              </template>
            </el-table-column>
            <el-table-column
                label="创建时间"
                align="center"
                prop="create_time"
                width="180"
            />
            <el-table-column label="操作" fixed="right" width="220px">
              <template slot-scope="scope">
                <el-button type="text" style="color: #F82222" @click="onReset(scope.row)">重置密码</el-button>
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>

    <Add ref="addFormRef" @get-page="initData" />
    <Role ref="addRoleRef" @get-page="initData" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add.vue";
import Role from "./role.vue";
import {CommonStatusEnum} from "@/utils/enum";
export default {
  name: "index",
  components: {
    Add,
    Role
  },
  mixins: [crud],
  data() {
    return {
      request:http.user,
      searchList: [
        {
          label: '姓名',
          prop: 'nickname',
          name: 'input',
          clearable: true
        },
        {
          label: '用户名',
          prop: 'username',
          name: 'input',
          clearable: true
        },
        {
          label: '手机号',
          prop: 'mobile',
          name: 'input',
          clearable: true
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          clearable: true,
          options: [
            { label: '启用', value: CommonStatusEnum.ENABLE },
            { label: '禁用', value: CommonStatusEnum.DISABLE }
          ]
        },
        {
          label: '创建时间',
          prop: 'createTime',
          name: 'date',
          clearable: true,
          type: 'daterange'
        }
      ],
      searchParams: {
        nickname: '',
        username: undefined,
        mobile: undefined,
        status: undefined,
        deptId: undefined,
        createTime: []
      },
      tableData: [

      ]
    }
  },
  methods: {
    async handleStatusChange(row) {
      try {
        // 发起修改状态
        await http.user.update(row.id, {
          status: row.status
        })
        // 刷新列表
        await this.onSearch()
      } catch {
        // 取消后，进行恢复按钮
        row.status =
            row.status === CommonStatusEnum.ENABLE ? CommonStatusEnum.DISABLE : CommonStatusEnum.ENABLE
      }
    },
    onReset(row) {
      // 对话框输入密码
      this.$prompt('请输入密码', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: 'password',
        showPassword: true,
        inputValidator: (value) => {
          if (!value) {
            return '密码不能为空'
          }
        }
      }).then(({ value }) => {
        // 发起重置密码
        http.user.update(row.id,{
          password: value
        }).then(() => {
          this.$message.success('重置密码成功')
        })
      }).catch(() => {
        this.$message.info('取消重置密码')
      })
    },
    onRole(row) {
      this.$refs.addRoleRef.open(row)
    }

  }
}
</script>

<style scoped lang="scss">

</style>
